package com.sync.qustion;


/**
 * 同时访问静态synchronized 和非静态synchronized 方法
 * */
public class SimpleMethodNoStatic4 implements Runnable {

    static SimpleMethodNoStatic4 simpleMethodNoStatic1 = new SimpleMethodNoStatic4();
    static SimpleMethodNoStatic4 simpleMethodNoStatic2 = new SimpleMethodNoStatic4();

    static int count = 0;
    public static void main(String[] args) {
        Thread t1 = new Thread(simpleMethodNoStatic1);
        Thread t2 = new Thread(simpleMethodNoStatic2);
        t1.start();
        t2.start();
        while(t1.isAlive() || t2.isAlive());
        System.out.println(" finish " +count);

    }

    @Override
    public void run() {
        method();
        cast();
    }

    // synchronized 修饰方法的时候默认里面对象是this对象
    public static synchronized void method(){
        System.out.println("synchronized" +Thread.currentThread().getName()+"静态加锁的方法");
        for (int i = 0; i < 100000; i++) {
            count++;
        }
        System.out.println("synchronized " +Thread.currentThread().getName()+"运行结束");

    }
    // synchronized 修饰方法的时候默认里面对象是this对象
    public synchronized void cast(){
        System.out.println(Thread.currentThread().getName()+"非静态加锁的方法哦");
        for (int i = 0; i < 100000; i++) {
            count--;
        }
        System.out.println(Thread.currentThread().getName()+"运行结束");

    }
}
